package com.nyist.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nyist.entity.Manage;
import com.nyist.entity.Relation;
import com.nyist.service.ManageService;
import com.nyist.service.RelationService;
import com.nyist.util.SendMail;
/**
 * 用户注册
 * 1，注册的时候验证码的判断，然后将数据存入，并发送邮箱验证信息（reg）
 * 2，在发送的邮件中有一个路径然后点击路径进入 该Controller 然后将邮箱验证的状态由0变成1表示已经验证了
 * @author 18077
 *
 */
@Controller
@RequestMapping(value="manage")
public class ManageRegisterController {
	@Autowired
	@Qualifier("manageService")
	private ManageService manageService;
	@Autowired
	@Qualifier("relationService")
	private RelationService relationService;
	@RequestMapping(value="reg")
	public void reg(Manage manage,HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setContentType("text/html;charset=utf-8");
		/*验证码判断*/
		String code = (String)request.getSession().getAttribute("validationCode");
		String code1 = manage.getValidatecode();
		if(code.equals(code1)){
			/*将用户的信息存进数据库*/
			String id = manageService.addManage(manage);
			System.out.println(manage+"control------------------");
			/*将用户和角色关系存进数据库，用户刚注册的时候都是普通用户，然后需要超级管理员进行管理*/
			Relation relation = new Relation();
			relation.setManageid(id);
			relation.setRoleid(3);
			System.out.println(relation);
			relationService.insertRelation(relation);
			/*组装 响应的url */
			 //获取IP
	        InetAddress addr = InetAddress.getLocalHost();
			String ip=addr.getHostAddress().toString();
	        String url = "http://";
	        url+=ip+":";
	        url+=request.getLocalPort();
	        url+=request.getContextPath();
	        url+="/manage/activation.do";//需要修改
	        url += "?id=" + id + "&name="+ manage.getUsername();
	        
	        /*调用函数，邮箱认证*/
	        //new SendMail().sendMail(manage.getMail(), url);
	        
	        PrintWriter out = response.getWriter();
	        out.print("<h3 align='center'>用户注册完成，激活帐号邮件已经发出，请您登陆您的邮箱按照信中地址激活您帐号</h3>");
	        out.print("<br><center><a href='/RegistLogin/login.jsp'>返回首页</a></center>");
	        out.close();
		}
		else{
			response.sendRedirect("/RegistLogin/regist.jsp");
		}
	}
	//邮件点击后返回到这，将验证的状态修改
	@RequestMapping(value="activation")
	public void activation(HttpServletRequest request,HttpServletResponse response) throws IOException{
			response.setContentType("text/html;charset=utf-8");
			/*根据响应的url地址中携带的参数 可以获取到id 和name 然后将将验证码的状态修改，改为已验证*/
			
	        String id = request.getParameter("id");
	        String username = request.getParameter("name");
	        manageService.updateState(id, username);
	        PrintWriter out = response.getWriter();
	        
	        out.print("<br><br>");
	        out.print("<center>用户已被激活</center>");
	        out.print("<br><center><a href='/RegistLogin/login.jsp'>进入登录页面</a></center>");
	        out.close();
	}
	/*处理 Ajax 请求，判断用户名是否已经被使用过了，使用username 进行数据库的查找如果找到了和username 一样的用户名那么该用户名不能使用了*/
	@RequestMapping(value="checkUsername",method=RequestMethod.GET)
	public void checkUsername(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String username = request.getParameter("username");
		System.out.println(username);
		PrintWriter out = response.getWriter();
		Manage manage = manageService.queryForRegist(username);
		System.out.println(manage);
		if(manage!=null){
			out.print(0);
		}else{
			out.print(1);
		}
	}
	
}
